package org.jrad.core;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.jrad.model.Attribute;
import org.jrad.model.EntityCrud;
import org.jrad.model.Project;
import org.jrad.util.DAOHelper;
import org.jrad.util.EntityHelper;
import org.jrad.util.JRadHelper;
import org.jrad.util.JSFHelper;
import org.jrad.util.MBHelper;
import org.jrad.util.ParserXML;

public class Engine {

	private static Project project;
	private static String PROJECT_PATH = "c:\\jRAD";
	private static final Attribute ID = new Attribute("id", "long", "10", "",true);
	private static File rootSrc;
	private static File rootMetaInf;

	public static void doCrud(String projectPath) {
		if(!projectPath.equals(""))
			PROJECT_PATH = projectPath;
		try {
			parseXML();
//			cleanProject();
//			createProject();
//			createPersistenceUnit();
//			createGenericDAO();
//			createEntities();
//			createMB();
//			createJSF();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void cleanProject() {
		try {
			File proj = new File(PROJECT_PATH+File.separator+project.getName());		
			if(proj.exists()) {
				FileUtils.deleteDirectory(proj);			
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void createJSF() {
		try {
			String webPath = PROJECT_PATH + File.separator + project.getName() + File.separator + "WebContent";
			JSFHelper.createWebContent("template" + File.separator + "WebContent", webPath);
			JSFHelper.changeTemplates(webPath,project.getName(),project.getCruds());			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void createMB() {
		File mbPath = new File(rootSrc.getAbsolutePath()+File.separator+"mb");
		mbPath.mkdir();
		List<EntityCrud> cruds = project.getCruds();
		for (EntityCrud entityCrud : cruds) {			
			try {
				JRadHelper.writeFile(mbPath.getAbsoluteFile()+File.separator+
						EntityHelper.upFirst(entityCrud.getName())+"MB.java", MBHelper.createMB(project.getPackageRoot(),entityCrud));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static void createGenericDAO() {		
		File daoPath = new File(rootSrc.getAbsolutePath()+File.separator+"dao");
		daoPath.mkdirs();
		try {
			JRadHelper.writeFile(daoPath.getAbsoluteFile()+File.separator+"GenericDAO.java",
					DAOHelper.createDAO(project.getPackageRoot()+".dao"));
			
			JRadHelper.writeFile(daoPath.getAbsoluteFile()+File.separator+"Connection.java",
					DAOHelper.createConnection(project.getPackageRoot()+".dao",	project.getPersistenceUnitData().getName()));
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

	private static void createPersistenceUnit() {
		rootMetaInf = new File(PROJECT_PATH+File.separator+project.getName()+File.separator+"src"+File.separator+"META-INF");
		rootMetaInf.mkdirs();
		try {
			JRadHelper.writeFile(rootMetaInf.getAbsoluteFile()+File.separator+"persistence.xml",DAOHelper.createPersistenceUnitData(project.getPersistenceUnitData()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void createProject() {
		String packageRootPath = project.getPackageRoot().replaceAll("\\.", "\\"+File.separator);
		rootSrc = new File(PROJECT_PATH+File.separator+project.getName()+File.separator+"src"+File.separator+packageRootPath);		
		rootSrc.mkdirs();
	}

	private static void parseXML() throws Exception{
		project = ParserXML.parserXML("project.xml","project.xsd");		
	}

	private static void createEntities() {
		File entityPath = new File(rootSrc.getAbsolutePath()+File.separator+"entity");
		entityPath.mkdir();
		List<EntityCrud> cruds = project.getCruds();
		for (EntityCrud entityCrud : cruds) {
			StringBuilder output = new StringBuilder();
			List<Attribute> attributes = entityCrud.getAttributes();
			String classTemplate = EntityHelper.createEntity(entityCrud,project.getPackageRoot());
			output.append(EntityHelper.createAttributeId());
			output.append(EntityHelper.createAttributes(attributes));
			output.append(EntityHelper.createGetMethod(ID));
			output.append(EntityHelper.createSetMethod(ID));
			for (Attribute attribute : attributes) {				
				output.append(EntityHelper.createGetMethod(attribute));
				output.append(EntityHelper.createSetMethod(attribute));
			}			
			String classContent = classTemplate.replaceFirst("<content>", output.toString());			
			try {			
				JRadHelper.writeFile(entityPath.getAbsolutePath()+File.separator+entityCrud.getName()+".java",classContent);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		doCrud(args.length > 0 ? args[0]:"");
	}
}
